package xyz.weblade.jdbc.support;

import cn.hutool.core.lang.Assert;
import xyz.weblade.beans.factory.InitializingBean;

import javax.sql.DataSource;

public abstract class JdbcAccessor implements InitializingBean {

    private DataSource dataSource;


    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public DataSource getDataSource() {
        return this.dataSource;
    }


    protected DataSource obtainDataSource() {
        DataSource dataSource = getDataSource();
        Assert.state(dataSource != null, "No DataSource set");
        return dataSource;
    }


    @Override
    public void afterPropertiesSet() {
        if (getDataSource() == null) {
            throw new IllegalArgumentException("Property 'dataSource' is required");
        }
    }

}
